package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jinpei
 * @version v1.0
 * @date 2019/5/6 15:42
 * @description TODO
 **/
@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌
     * @param page
     * @param rows
     * @param sortBy
     * @param desc
     * @param key
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "page" ,defaultValue = "1")Integer page,
            @RequestParam(value = "rows" ,defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy" ,required = false)String sortBy,
            @RequestParam(value = "desc" ,defaultValue = "false")Boolean desc,
            @RequestParam(value = "key" ,required = false)String key

    ) {
        return ResponseEntity.ok(brandService.queryBrandByPage(page, rows, sortBy, desc, key));
    }

    /**
     * 新增品牌
     * @param brandDTO  品牌对象
     * @param ids  品牌相关分类的id
     * @return 无
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> ids) {
        brandService.saveBrand(brandDTO,ids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 跟新品牌
     * @param brandDTO 品牌对象
     * @param ids 品牌分类id
     * @return 无
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> ids) {
        brandService.updateBrand(brandDTO, ids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据分类id查询品牌
     * @param id 分类id
     * @return 品牌的集合
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategoryId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(brandService.queryBrandByCategoryId(id));
    }

    /**
     * 根据品牌id查询品牌
     * @param id 品牌id
     * @return 品牌对象
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(brandService.queryById(id));
    }

    /**
     * 根据品牌的id批量查询品牌
     * @param idList 品牌id的集合
     * @return 品牌的集合
     */
    @RequestMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> idList){
        return ResponseEntity.ok(brandService.queryBrandByIds(idList));
    }


}
